﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Data;
using QLKS.Models;
using System.ComponentModel;

namespace QLKS.ViewModels
{
    public class TangViewModel : ViewModelBase
    {
        int _tang;
        ObservableCollection<PhongViewModel> _dsPhong;
        int _selectedIndex = -1;

        public TangViewModel(int tang)
        {
            _tang = tang;
            _dsPhong = new ObservableCollection<PhongViewModel>();
            foreach (PhongModel ph in PhongModel.LayDSPhongThuocTang(tang))
            {
                PhongViewModel vm = new PhongViewModel(ph);
                vm.PropertyChanged += OnPhongPropertyChanged;
                _dsPhong.Add(vm);
            }
        }

        public int Tang { get { return _tang; } }
        public ObservableCollection<PhongViewModel> DsPhong { get { return _dsPhong; } }

        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                _selectedIndex = value;
                OnPropertyChanged("SelectedIndex");
            }
        }

        private void OnPhongPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            string property = "IsSelected";
            PhongViewModel vm = sender as PhongViewModel;
            if (e.PropertyName == property && vm.IsSelected)
            {
                OnPropertyChanged("IsSelected");
            }
        }
    }
}
